package org.minos.edit.view.components
{
	import flash.events.Event;
	import org.aswing.ASColor;
	import org.aswing.BorderLayout;
	import org.aswing.CellEditor;
	import org.aswing.DefaultTextFieldCellEditor;
	import org.aswing.event.ListDataEvent;
	import org.aswing.event.ListDataListener;
	import org.aswing.event.SelectionEvent;
	import org.aswing.JPanel;
	import org.aswing.JScrollPane;
	import org.aswing.JTable;
	import org.aswing.table.PropertyTableModel;
	import org.aswing.table.sorter.TableSorter;
	import org.aswing.table.TableCellEditor;
	import org.aswing.VectorListModel;
	import org.ichow.utils.XMLUtil;
	import org.minos.edit.model.vo.ItemVO;
	import org.minos.edit.view.components.interfaces.IFile;
	
	/**
	 * ...
	 * @author Minos
	 */
	public class SceneTable extends JPanel implements IFile
	{
		public static const LABEL_CHANGED : String = "labelChanged";
		
		private var table : JTable;
		private var scrollPane : JScrollPane;
		private var taskList : VectorListModel;
		private var tableModel : PropertyTableModel;
		private var sorter : TableSorter;
		
		public function SceneTable()
		{
			super();
			init();
		}
		
		/**
		 *
		 */
		private function init() : void
		{
			setLayout( new BorderLayout() );
			table = new JTable();
			scrollPane = new JScrollPane( table );
			append( scrollPane , BorderLayout.CENTER );
			
			setOpaque( true );
			setBackground( new ASColor( 0xE6EBEC ) );
			
			taskList = new VectorListModel();
			tableModel = new PropertyTableModel( taskList , [ "名称" , "帧数" , "子菜单" , "动画路径" , "分数" , "鼠标提示" , "操作提示" ] , [ "label" , "frame" , "children" , "url" , "score" , "tooltip" , "info" ] , [ __labelTranslator ] );
			sorter = new TableSorter( tableModel );
			table.setModel( sorter );
			sorter.setTableHeader( table.getTableHeader() );
			tableModel.setColumnClass( 1 , "Number" );
			tableModel.setColumnEditable( 2 , false );
			tableModel.setColumnEditable( 3 , false );
			
			table.getColumnAt( 1 ).setPreferredWidth( 20 );
			table.getColumnAt( 4 ).setPreferredWidth( 10 );
			
			table.addEventListener( SelectionEvent.ROW_SELECTION_CHANGED , onRowSelectionChanged );
		
		}
		
		private function __labelTranslator( vo : ItemVO , key : String ) : *
		{
			this.dispatchEvent( new Event( SceneTable.LABEL_CHANGED ) );
			return vo;
		}
		
		private function onRowSelectionChanged( e : SelectionEvent ) : void
		{
			dispatchEvent( e );
		}
		
		public function getSelectedVO() : ItemVO
		{
			
			var row : int = table.getSelectedRow();
			var vo : ItemVO = null;
			if ( row >= 0 )
			{
				vo = taskList.getElementAt( sorter.modelIndex( row ) );
			}
			return vo;
		}
		
		/**
		 *
		 * @return
		 */
		public function getTable() : JTable
		{
			return table;
		}
		
		public function getList() : VectorListModel
		{
			return taskList;
		}
		
		/**
		 *
		 * @param	s
		 */
		public function addScene( s : ItemVO ) : void
		{
			if ( s )
				taskList.append( s );
		}
		
		/**
		 *
		 * @param	s
		 */
		public function removeScene( s : ItemVO ) : void
		{
			if ( s )
				taskList.remove( s );
		}
		
		public function change( s : ItemVO ) : void
		{
			if ( s )
				taskList.valueChanged( s );
		}
		
		public function getRowCount() : int
		{
			return table.getRowCount();
		}
		
		public function getItems() : Array
		{
			return taskList.toArray();
		}
		
		/* INTERFACE org.minos.edit.view.components.interfaces.IFile */
		
		public function clear() : void
		{
			taskList.clear();
		}
		
		public function getXML() : XML
		{
			setID();
			var items : Array = getItems();
			var xml : XML =               <items/>;
			for ( var i : int = 0 , len : int = items.length ; i < len ; i++ )
			{
				var vo : ItemVO = items[ i ];
				var _xml : XML = <item />;
				_xml.@id = vo.id;
				_xml.@title = vo.label;
				_xml.@icon = vo.frame;
				_xml.@type = vo.type;
				_xml.@children = ( vo.type == 0 ) ? getID( vo.children ) : "";
				_xml.@url = vo.url;
				_xml.@score = vo.score;
				_xml.@des = vo.tooltip;
				_xml.@info = vo.info;
				_xml.appendChild( XMLUtil.CDATA( "continueValue" , vo.continueInfo ) );
				_xml.appendChild( XMLUtil.CDATA( "endValue" , vo.endInfo ) );
				xml.appendChild( _xml );
			}
			return xml;
		}
		
		public function setXML( xml : Object ) : void
		{
			var _xml : XML = new XML( xml );
			var vos : Array = [];
			for ( var i : int = 0 , len : int = _xml.item.length() ; i < len ; i++ )
			{
				var vo : ItemVO = new ItemVO();
				vo.id = _xml.item[ i ].@id;
				vo.label = _xml.item[ i ].@title;
				vo.frame = _xml.item[ i ].@icon;
				vo.type = _xml.item[ i ].@type;
				vo.children = ( vo.type == 0 ) ? _xml.item[ i ].@children.split( "," ) : [];
				vo.url = _xml.item[ i ].@url;
				vo.score = _xml.item[ i ].@score;
				vo.tooltip = _xml.item[ i ].@des;
				vo.info = _xml.item[ i ].@info;
				vo.continueInfo = _xml.item[ i ].continueValue;
				vo.endInfo = _xml.item[ i ].endValue;
				vos.push( vo );
			}
			taskList.appendAll( vos );
			setChildren();
		}
		
		/**
		 *
		 * @param	ids
		 * @return
		 */
		private function setChildren() : void
		{
			var items : Array = getItems();
			for ( var i : int = 0 , len : int = items.length ; i < len ; i++ )
			{
				var vo : ItemVO = items[ i ];
				var ary : Array = [];
				for each ( var j : Object in vo.children )
				{
					if ( j != "" )
						ary.push( taskList.getElementAt( int( j ) ) );
				}
				vo.children = ary;
			}
		}
		
		/**
		 *
		 * @param	ary
		 * @return
		 */
		private function getID( ary : Array ) : Array
		{
			var ids : Array = [];
			for each ( var i : ItemVO in ary )
			{
				if ( i )
					ids.push( i.id );
			}
			return ids;
		}
		
		/**
		 *
		 */
		private function setID() : void
		{
			var items : Array = getItems();
			for ( var i : int = 0 , len : int = items.length ; i < len ; i++ )
			{
				var vo : ItemVO = items[ i ];
				vo.id = i;
			}
		}
	
	}

}